package com.sky.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.*;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
@AllArgsConstructor
@Slf4j
public class BaiDuUtils {
    //商家地址
    private String address;
    //百度授权key
    private String baiduKey;

    /**
     * 通过geodesy来计算两个经纬度之间的距离
     * @param cosAdress
     * @return
     */
    public Double mathGPS(String cosAdress){
        List<Double> area = new ArrayList();
        List<Double> bus = new ArrayList<>();
        try {
            area = baiDuAddressChange(cosAdress);
            bus = baiDuAddressChange(address);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //给定两个坐标系,计算两点相差距离
        GlobalCoordinates source = new GlobalCoordinates(bus.get(0), bus.get(1));
        GlobalCoordinates target = new GlobalCoordinates(area.get(0), area.get(1));
        //Sphere坐标的计算结果
        double meter1 =getDistanceMeter(source,target,Ellipsoid.Sphere);
        //WGS84坐标系计算结果
        double meter2 = getDistanceMeter(source,target,Ellipsoid.WGS84);

        //计算结果Sphere 坐标系的计算结果与 WGS84坐标系的计算结果存在几十米的误差，不同的坐标系精度不同,
        System.out.println("Sphere 坐标系的计算结果是" + meter1 + "米");
        System.out.println("wGS84  坐标系的计算结果是" + meter2 + "米");
        return null;
    }
    //计算的方法
    private double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){
        //创建GeodeticCalculator,调用计算方法,传入坐标系,经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
        return geoCurve.getEllipsoidalDistance();
    }

    //百度api的经纬转换方法
    private List baiDuAddressChange(String address) throws IOException {
        CloseableHttpClient aDefault = HttpClients.createDefault();
        // 访问示例 https://api.map.baidu.com/geocoding/v3/?address=北京市海淀区上地十街10号&output=json&ak=您的ak&callback=showLocation //GET请求
        HttpGet httpGet = new HttpGet("https://api.map.baidu.com/geocoding/v3/?address=" + address + "&output=json&ak=" + baiduKey  +
                "&callback=showLocation"  );
        System.out.println(httpGet.getURI());
        CloseableHttpResponse response = aDefault.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        log.info("连接码是{}",statusCode);
        System.out.println(statusCode);
        HttpEntity entity = response.getEntity();
        String s = EntityUtils.toString(entity);
        System.out.println(s);
        Pattern compile = Pattern.compile("\\d+\\.\\d{5,}");
        Matcher matcher = compile.matcher(s);
        List<Double> area = new ArrayList<>();
        while (matcher.find()) {
            String group = matcher.group();
            Double aDouble = Double.valueOf(group);
            area.add(aDouble);
        }
        return  area;
    }


}
